package com.yx.front.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.front.config.global.CommonResult;
import com.yx.front.enums.IntBoolean;
import com.yx.front.enums.TimeFormat;
import com.yx.front.model.NoticeUser;
import com.yx.front.model.SysUser;
import com.yx.front.model.SystemNotice;
import com.yx.front.model.base.BaseParam;
import com.yx.front.model.vo.NoticeUserVo;
import com.yx.front.service.NoticeUserService;
import com.yx.front.service.SystemNoticeService;
import com.yx.front.utils.LoginUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yangx
 */
@Component
public class NoticeUserManager {
    @Resource
    private NoticeUserService noticeUserService;
    @Resource
    private SystemNoticeService systemNoticeService;
    @Resource
    private LoginUtil loginUtil;

    public CommonResult<List<NoticeUserVo>> getList(BaseParam param) {
        SysUser currentUser = loginUtil.getCurrentUser();
        Page<NoticeUser> page = new Page<>(param.getCurrentPage(), param.getTotalPage());
        LambdaQueryWrapper<NoticeUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NoticeUser::getToId, currentUser.getId());
        wrapper.orderByDesc(NoticeUser::getCreateTime);
        Page<NoticeUser> userPage = noticeUserService.page(page, wrapper);
        if(CollectionUtil.isEmpty(userPage.getRecords())){
            return CommonResult.success(Collections.emptyList(), 0L);
        }
        List<Long> ids = userPage.getRecords().stream().map(NoticeUser::getNoticeId).collect(Collectors.toList());
        //查询通知表
        LambdaQueryWrapper<SystemNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SystemNotice::getId, ids);
        Map<Long, SystemNotice> noticeMap = systemNoticeService.list(queryWrapper).stream().collect(Collectors.toMap(SystemNotice::getId, Function.identity()));
        List<NoticeUserVo> result = handlerResult(userPage.getRecords(), noticeMap);
        return CommonResult.success(result, userPage.getTotal());
    }

    private List<NoticeUserVo> handlerResult(List<NoticeUser> list, Map<Long, SystemNotice> noticeMap) {
        List<NoticeUserVo> vos = new ArrayList<>();
        list.forEach(one->{
            NoticeUserVo vo = new NoticeUserVo();
            vo.setId(one.getId());
            SystemNotice notice = noticeMap.get(one.getNoticeId());
            vo.setNoticeContext(notice.getContext());
            vo.setCreateTime(DateUtil.format(notice.getCreateTime(), TimeFormat.TIME_FORMAT));
            vo.setModifyTime(DateUtil.format(notice.getModifyTime(), TimeFormat.TIME_FORMAT));
            vo.setStatus(one.getStatus());
            vo.setOperatorName(notice.getOperatorName());
            vos.add(vo);
        });
        return vos;
    }

    public CommonResult<String> read(Long id) {
        LambdaUpdateWrapper<NoticeUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(NoticeUser::getId, id)
                .set(NoticeUser::getStatus, IntBoolean.YES.getKey());
        boolean update = noticeUserService.update(wrapper);
        if (update){
            return CommonResult.success("消息已读");
        }
        return CommonResult.fail("消息读取错误");
    }
}
